<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/7/19
 * Time: 14:13
 */
/**
 * Created by wanglu on 2017/7/19.
 */
class User extends AdminBase {

    function __construct() {
        parent::__construct();
        $this->load->service('user_service');
    }


    public function users() {
        if ($this->is_post()) {

            $search = $this->input->post("search");
            $order = $this->input->post("order");
            $order_by_createtime =  $order[0]["dir"];
            $searchValue = $search['value'];

            $start = $this->input->post("start");
            $length = $this->input->post("length");
            $data = $this ->user_service->get_users($start, $length, $searchValue, $order_by_createtime);

            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]["createtime"] = TimeUtil::showtime($data['data'][$key]["createtime"] , TimeUtil::TIME_ZONE_GMT8);
                //mdate("%Y-%m-%d  %h:%i:%s", TimeUtil::convertUTC2GMTTimestamp($data['data'][$key]["createtime"]));
            }
            echo json_encode($data);
        } else {
            $this->showView("user_list");
        }

    }

    public function admin_list() {
        $this->showView("user_list");
    }

    public function slightech_list() {
        $this->showView("user_slightech_list");
    }


    public function edit() {
        $username  = $this->input->post("username");
        $pwd = $this->input->post("pwd");
        $email = $this->input->post("email");
        $role = $this->input->post("role");
        $id = $this->input->post("id");
        $res =  $this->user_service->add_or_update_user($username, $pwd, $role, $email, $id);

        if ($res) {
            die(json_encode(array("code" => 200)));
        } else {
            die(json_encode(array("code" => 201, "msg"=> '操作失败')));
        }
    }

    public function delete() {
        $id  = $this->input->post("id");
        $res  = $this ->user_service->delete_user_by_id($id);

        if ($res) {
            echo json_encode(array("code" => 200));
            exit;
        }

        echo json_encode(array("code" => 400000, 'msg' => "删除用户失败"));
    }


    public function password() {
        if ($this->is_post()) {
            $oldpassword = $this->input->post("old_pwd");
            $newpassword = $this->input->post("new_pwd");
            if ($this->user_service->update_password($this->loginUser['id'], $oldpassword, $newpassword)) {
                echo json_encode(array("code" => 200));
            } else {
                die(json_encode(array("code" => 201, "msg"=> '操作失败')));
            }
        } else {
            $this->showView("update_password");
        }
    }


    function get_e($str) {
            $result = array();

        preg_match_all("/(?:\()(.*)(?:\))/i",$str, $result);


            return $result[1][0];
    }

    public function getemail() {
        if ($this->is_post()) {
            $email = $this->input->post('email');
            $email_array = explode('[取消关注]', $email);



            //echo json_encode($email_array);
            //exit;

            foreach ($email_array as $e) {

                $user_e = $this->get_e($e) . "@qq.com";
                echo '' .$user_e ;
                echo "<br/>";
            }


            //echo $email;
            exit;
        } else {
            $this->showView("getemail");
        }

    }
}